//
//  FPFeedBackController.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/1/24.
//

#import "FPFeedBackController.h"

@interface FPFeedBackController () <UITextFieldDelegate , UITextViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *backView;
@property (weak, nonatomic) IBOutlet UILabel *tipLab1;
@property (weak, nonatomic) IBOutlet UILabel *tipLab2;
@property (weak, nonatomic) IBOutlet UIView *lineView;
@property (weak, nonatomic) IBOutlet UITextField *titleTF;
@property (weak, nonatomic) IBOutlet FPCustomTextView *textView;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;

@property (nonatomic , strong) NSString *paramTitle;
@property (nonatomic , strong) NSString *paramContent;

@end

@implementation FPFeedBackController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)initUI {
    
    self.lineView.backgroundColor = [UIColor hexColor:@"#DEDEDE"];
    self.tipLab1.textColor = [UIColor hexColor:@"#3A3A3A"];
    self.tipLab1.textColor = [UIColor hexColor:@"#3A3A3A"];
    self.confirmBtn.clipsToBounds = YES;
    self.confirmBtn.layer.cornerRadius = 20.f;
    self.confirmBtn.backgroundColor = MAIN_BLUE_COLOR;
    
    self.view.backgroundColor = [UIColor hexColor:@"#F4F6FA"];
    [self setTitle:@"意见反馈" style:(XYCustomNavigationStyleWhite) hiddenBackBtn:NO];
    
    self.backView.clipsToBounds = YES;
    self.backView.layer.cornerRadius = 8.f;
    
    self.titleTF.delegate = self;
    self.textView.placeHolder = @"请输入内容...";
    self.textView.delegate = self;
}



- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    if ([string isEqualToString:@""]) {
        
        return YES;
    }
    
    if ([textField isEqual:self.titleTF]) {
        
        return textField.text.length < 30;
    }
    
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    self.paramTitle = textField.text;
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    
    self.paramContent = textView.text;
}

- (IBAction)confirmAction:(UIButton *)sender {
    
    if ([NSString isBlankString:self.paramTitle]) {
        
        FeedBackGeneratorAction
        [self.titleTF shake];
        return;
    }
    
    if ([NSString isBlankString:self.paramContent]) {
        
        FeedBackGeneratorAction
        [self.textView shake];
        return;
    }
    
    if ([NSString isContainsEmoji:self.paramTitle]) {
        
        [self showText:@"包含非法字符"];
        return;
    }
    
    if ([NSString isContainsEmoji:self.paramContent]) {
        
        [self showText:@"包含非法字符"];
        return;
    }
    
    NSMutableDictionary *param = APIClient.creatAPIDictionary;
    [param setObject:self.paramTitle forKey:@"headline"];
    [param setObject:self.paramContent forKey:@"substance"];
    
    [MBManager showLoading];
    [FPUserModel submitFeedBackWithParam:param success:^(NSInteger code, NSString * _Nonnull message) {
        
        [MBManager hideLoading];
        
        if (code == 200) {
            
            [self showText:@"提交成功"];
            [self performSelector:@selector(backAction) withObject:nil afterDelay:1.5f];
        }
        else {
            
            [self showText:message];
        }
        
    } failure:^(NSError * _Nonnull error) {
        [MBManager hideLoading];
    }];
}

@end
